package com.eastcom.auth.action.utils;

import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;

public class Utils {

//	//public static String IP_FILE=(Utils.class.getResource("")+"ip.dat").substring(6);
//	
//	public static String IP_FILE="";
//
//	static {
//		
//		if(System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
//			IP_FILE=(Utils.class.getResource("")+"ip.dat").substring(6);
//		}else{
//			IP_FILE=(Utils.class.getResource("")+"ip.dat").substring(5);
//		}
//	}
	
    public static String getIpStringFromBytes(byte[] ip) {
    	StringBuilder sb = new StringBuilder();
	    sb.delete(0, sb.length());
    	sb.append(ip[0] & 0xFF);
    	sb.append('.');   	
    	sb.append(ip[1] & 0xFF);
    	sb.append('.');   	
    	sb.append(ip[2] & 0xFF);
    	sb.append('.');   	
    	sb.append(ip[3] & 0xFF);
    	return sb.toString();
    }
    

    public static byte[] getIpByteArrayFromString(String ip) {
        byte[] ret = new byte[4];
        StringTokenizer st = new StringTokenizer(ip, ".");
        try {
            ret[0] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
            ret[1] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
            ret[2] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
            ret[3] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static String getString(String s, String srcEncoding, String destEncoding) {
        try {
            return new String(s.getBytes(srcEncoding), destEncoding);
        } catch (UnsupportedEncodingException e) {
            return s;
        }
    }

    public static String getString(byte[] b, int offset, int len, String encoding) {
        try {
            return new String(b, offset, len, encoding);
        } catch (UnsupportedEncodingException e) {
            return new String(b, offset, len);
        }
    }
}
